package com.shundr.auth.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.shundr.auth.R;
import com.shundr.auth.base.BaseActivity;
import com.shundr.auth.common.adapter.ChoiceAreaAdapter;
import com.shundr.auth.common.model.AreaInfo;
import com.shundr.auth.common.view.CListView;
import com.shundr.auth.frame.constant.LContext;
import com.shundr.auth.frame.util.JsonUtil;
import com.shundr.auth.frame.util.LLogger;
import com.shundr.auth.frame.util.StringUtil;
import com.shundr.auth.frame.util.ToastHelper;
import com.shundr.auth.user.dao.AreaDao;

public class ChoiceMainAreaActivity extends BaseActivity {

	private LinearLayout mLayoutChoiced;
	private TextView tvNotice;

	private CListView mListViewP;// 省
	private CListView mListViewC;// 市
	private CListView mListViewA;// 区

	private List<AreaInfo> mListP = new ArrayList<AreaInfo>();
	private List<AreaInfo> mListC = new ArrayList<AreaInfo>();
	private List<AreaInfo> mListA = new ArrayList<AreaInfo>();

	// private List<AreaInfo> selected = new ArrayList<AreaInfo>();
	private String detailInfo[] = new String[2];
	private String areaCode = "";

	private ChoiceAreaAdapter mAdapterP;
	private ChoiceAreaAdapter mAdapterC;
	private ChoiceAreaAdapter mAdapterA;

	private AreaDao mDao;

	private boolean isMul;
	private int max = 100;// 最多可选数目
	private boolean isCountry = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choice_area);
		String title = getIntent().getStringExtra("title");
		if (!StringUtil.isNull(title)) {
			setTitle(title);
		}
		isMul = getIntent().getBooleanExtra("is_mul", false);
		max = getIntent().getIntExtra("max", 100);
		isCountry = getIntent().getBooleanExtra("country", true);
		mDao = new AreaDao();
		mListP.addAll(mDao.getAreasByParentWithoutAll("00"));
		if (!isCountry && mListP.size() > 0) {
			mListP.remove(0);
		}
		initView();

		findViewById(R.id.divider_3).setVisibility(View.GONE);
		findViewById(R.id.ll_area).setVisibility(View.GONE);
		
		btnRight.setVisibility(View.VISIBLE);
		btnRight.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (StringUtil.isNull(areaCode)) {
					ToastHelper.toastShort(mContext, "请选择所在省和市！");
					return;
				}

				Intent intent = new Intent();
				intent.putExtra("addr", detailInfo[0] + " " + detailInfo[1]);
				intent.putExtra("areaCode", areaCode);
				setResult(502, intent);
				finish();
			}
		});
	}

	private void initView() {
		mLayoutChoiced = (LinearLayout) findViewById(R.id.layout_choiced);
		tvNotice = (TextView) findViewById(R.id.tv_notice);
		tvNotice.setText("请选择");

		mListViewP = (CListView) findViewById(R.id.lv_province);
		mListViewC = (CListView) findViewById(R.id.lv_city);
		mListViewA = (CListView) findViewById(R.id.lv_area);

		mAdapterP = new ChoiceAreaAdapter(mListP, mContext, false);
		mAdapterC = new ChoiceAreaAdapter(mListC, mContext, false);
		mAdapterA = new ChoiceAreaAdapter(mListA, mContext, true);

		mListViewP.setAdapter(mAdapterP);
		mListViewC.setAdapter(mAdapterC);
		mListViewA.setAdapter(mAdapterA);

		mListViewP.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				AreaInfo info = mListP.get(position - 1);
				for (AreaInfo areaInfo : mListP) {
					areaInfo.setSelected(false);
				}
				info.setSelected(true);

				// del all info
				detailInfo[0] = info.getName();
				detailInfo[1] = areaCode = "";

				initSelectedView();

				mAdapterP.notifyDataSetChanged();
				if (StringUtil.isEqual(info.getCode(), LContext.nullStr)) {
					initList(info);
					mListC.clear();
					mAdapterC.notifyDataSetChanged();

					mListA.clear();
					mAdapterA.notifyDataSetChanged();
				} else {
					mListC.clear();
					mListC.addAll(mDao.getAreasByParentWithoutAll(info
							.getCode()));
					mAdapterC.notifyDataSetChanged();

					mListA.clear();
					mAdapterA.notifyDataSetChanged();
				}
			}
		});

		mListViewC.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				AreaInfo info = mListC.get(position - 1);
				for (AreaInfo areaInfo : mListC) {
					areaInfo.setSelected(false);
				}

				detailInfo[1] = info.getName();
				areaCode = info.getCode();

				initSelectedView();

				info.setSelected(true);
				mAdapterC.notifyDataSetChanged();
				// if (StringUtil.isEqual(info.getCode(), info.getParentCode()))
				// {
				// initList(info);
				// mListA.clear();
				// mAdapterA.notifyDataSetChanged();
				// } else {
				// mListA.clear();
				// mListA.addAll(mDao.getAreasByParentWithoutAll(info.getCode()));
				// mAdapterA.notifyDataSetChanged();
				// }
			}
		});

		mListViewA.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				AreaInfo info = mListA.get(position - 1);
				for (AreaInfo areaInfo : mListA) {
					areaInfo.setSelected(false);
				}
				info.setSelected(true);
				mAdapterA.notifyDataSetChanged();
			}
		});

	}

	private void initList(AreaInfo info) {
		// boolean isContained = false;
		// for(AreaInfo areaInfo : selected){
		// if(StringUtil.isEqual(areaInfo.getCode(), info.getCode())){
		// isContained = true;
		// }
		// }
		// if(isContained){
		// ToastHelper.toastShort(mContext, "该地区已选择");
		// return;
		// }
		// if(selected.size() >= max){
		// ToastHelper.toastShort(mContext, "最多只能选择" + max + "个");
		// return;
		// }
		// if(!isMul){
		// selected.clear();
		// }
		// selected.add(info);
		initSelectedView();
	}

	/**
	 * 动态添加布局 initSelectedView(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选) void
	 * 
	 * @exception
	 * @since 1.0.0
	 */
	private void initSelectedView() {
		// mLayoutChoiced.removeAllViews();
		// for(int i = 0;i < selected.size();i++){
		// AreaInfo area = selected.get(i);
		// View view =
		// LayoutInflater.from(mContext).inflate(R.layout.view_selected_area,
		// null);
		// TextView tvName = (TextView)view.findViewById(R.id.tv_value);
		// view.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// int tag = (Integer)v.getTag();
		// mLayoutChoiced.removeViewAt(tag);
		// selected.remove(tag);
		// initSelectedView();
		// }
		// });
		// tvName.setText(area.getName());
		// view.setTag(i);
		// mLayoutChoiced.addView(view, i);
		// }
		// if(mLayoutChoiced.getChildCount() > 0){
		// tvNotice.setVisibility(View.GONE);
		// }else{
		// tvNotice.setVisibility(View.VISIBLE);
		// }

		tvNotice.setText(detailInfo[0] + " " + detailInfo[1]);

	}

}
